<?php
/// +----------------------------------------------------------------------
// | 99PHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2020 https://www.99php.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mr.Chung <chung@99php.cn >
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\common\controller\AdminController;
use think\facade\Cache;
use app\common\model\CompanyModel;
use app\common\model\AccountRecordModel;
use app\common\model\UserModel;
use app\common\model\EquipmentModel;

class Index extends AdminController {
    protected $CompanyModel;
    protected $AccountRecordModel;
    protected $UserModel;
    protected $EquipmentModel;


    // 初始化构造方法
    public function __construct()
    {
        parent::__construct();
        $this->CompanyModel = new CompanyModel();  // 企业模型
        $this->AccountRecordModel=new AccountRecordModel();//账户记录
        $this->UserModel=new UserModel();//用户模型
        $this->EquipmentModel=new EquipmentModel();//设备模型
    }

    /**
     * 首页
     * @return mixed
     */
    public function index() {
        Cache::remember('Home', function () {
            return model('menu')->getHome();
        });
        $basic_data = [
            'title' => Cache::get('SysInfo')['ManageName'],
            'nav'   => model('menu')->getNav(),
            'home'  => Cache::get('Home'),
            'data'  => '',
        ];
        return $this->fetch('', $basic_data);
    }

    /**
     * 首页界面
     * @return mixed
     */
    public function welcome() {
        //获取今日日期、星期、天气
        $data['today']=date('Y年m月d日 ');
        // 获取今日时间戳
        $today_start = strtotime(date('Y-m-d 00:00:00'));
        $today_end = strtotime(date('Y-m-d H:i:s'));
        $between = ['between', $today_start, $today_end];

        //获取总用户数
        $data['total_user']=$this->UserModel->getCount(['is_deleted'=>0]);

        //获取今日新增用户数
        $data['today_user']=$this->UserModel->getCount(['is_deleted'=>0,'create_time'=>$between]);

        //获取男性用户
        $data['male']=$this->UserModel->getCount(['is_deleted'=>0,'sex'=>1]);

        //获取女性用户
        $data['female']=$data['total_user']-$data['male'];

        //获取总企业数
        $data['total_company']=$this->CompanyModel->getCount();

        //今日新增企业
        $data['today_company']=$this->CompanyModel->getCount(['create_time'=>$between]);

        //获取总设备数
        $data['total_device']=$this->EquipmentModel->getCount();

        //获取今日新增设备
        $data['today_device']=$this->EquipmentModel->getCount(['create_time'=>$between]);

        //获取总的收入
        $where_income['type']=1;//增加
        $data['total_income']=formatMoney($this->AccountRecordModel->getSum($where_income,[],'money'),2);

        //今日新增收入
        $where_income['create_time']=$between;
        $data['today_income']=formatMoney($this->AccountRecordModel->getSum($where_income,[],'money'),2);

        //获取最近新增企业
        $data['new_company']=$this->CompanyModel->getAll([],'company_name,company_addr',1,3,[],'create_time desc');

        //获取设备数排行
        $join=[
            ['company c', 'c.company_no=a.company_no', 'left'],
        ];
        $data['rank_device']=$this->EquipmentModel->getAll([],'c.company_name,count(*) as num',1,10,$join,'num desc','c.company_id');

        //获取服务费排行
        $where_charge['source']=['IN', 3,4];
        $join=[
            ['account ac', 'ac.id=a.account_id', 'left'],
            ['company c', 'c.company_no=ac.company_no', 'left'],
        ];
        $data['rank_charge']=$this->AccountRecordModel->getAll($where_charge,'c.company_name,sum(a.money) as num',1,10,$join,'num desc','c.company_id');
        foreach($data['rank_charge'] as $k=>$v){
            $data['rank_charge'][$k]['num']=$v['num']?formatMoney($v['num'],2):0;
        }
        //获取设备数排行
        $join=[
            ['company c', 'c.company_id=a.company_id', 'left'],
        ];
        //获取用户数排行
        $data['rank_user']=$this->UserModel->getAll(['is_deleted'=>0],'c.company_name,count(*) as num',1,10,$join,'num desc','c.company_id');

        $basic_data = [
            'data'  => $data,
        ];
        return $this->fetch('',$basic_data);
    }


}